package com.chuangjiangx.member.stored.ddd.domain.service;

import com.chuangjiangx.domain.shared.model.Enable;
import com.chuangjiangx.member.coupon.ddd.domain.model.MbrConfig;
import com.chuangjiangx.member.coupon.ddd.domain.repository.MbrConfigRepository;
import com.chuangjiangx.member.basic.ddd.domain.model.MerchantId;
import com.chuangjiangx.member.stored.ddd.domain.model.IsDelete;
import com.chuangjiangx.member.stored.ddd.domain.model.MbrRechargeRuleId;
import com.chuangjiangx.member.stored.ddd.domain.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.Date;

/**
 * 储值规则
 *
 * @author 李孝延
 * @Date 2018/1/19 15:17
 **/
@Service
public class MbrRechargeRuleService {


    private final RechargeRuleRepository rechargeRuleRepository;
    private final MbrConfigRepository mbrConfigRepository;

    @Autowired
    public MbrRechargeRuleService(RechargeRuleRepository rechargeRuleRepository,  MbrConfigRepository mbrConfigRepository) {
        this.rechargeRuleRepository = rechargeRuleRepository;
        this.mbrConfigRepository = mbrConfigRepository;
    }

    /**
     * 创建储值规则
     *
     * @param createRechargeRule 参数
     * @throws Exception 异常
     */
    public void createRechargeRule(CreateRechargeRule createRechargeRule) throws Exception {
        Assert.notNull(createRechargeRule.getMerchantId(),"商户id不为空");
        //查询规则数量
        int count = rechargeRuleRepository.countByMerchantId(createRechargeRule.getMerchantId());
        MbrRechargeRule mbrRechargeRule = new MbrRechargeRule(
                createRechargeRule.getName(),
                createRechargeRule.getAmount(),
                createRechargeRule.getGiftScore(),
                createRechargeRule.getGiftAmount(),
                createRechargeRule.getCouponNumber(),
                createRechargeRule.getGiftCoupon(),
                createRechargeRule.getEnable(),
                createRechargeRule.getIsDelete(),
                createRechargeRule.getCreateTime(),
                createRechargeRule.getUpdateTime(),
                createRechargeRule.getMerchantId(),
                createRechargeRule.getMbrGiftType());

        //判断是否启用了10个
        if (count < 10) {
            int num = rechargeRuleRepository.rechargeRulesByName(mbrRechargeRule.getName(), new MerchantId(mbrRechargeRule.getMerchantId().getId()));
            //判断是否有重复的规则
            if (num == 0) {
                rechargeRuleRepository.save(mbrRechargeRule);
            } else {
                throw new Exception("有重复的储值规则");
            }

        } else {
            throw new Exception("新建储值规则启用的规则已经超过10个");
        }
    }


    /**
     * 启用储值规则
     *
     * @param enableRechargeRule 参数
     * @throws Exception 异常
     */
    public void enableRechargeRule(EnableRechargeRule enableRechargeRule) throws Exception {
        //查询规则数量
        int count = rechargeRuleRepository.countByMerchantId(new MerchantId(enableRechargeRule.getMerchantId()));

        //根据商户id 和储值规则id 查询储值规则
        MbrRechargeRule mbrRechargeRule = rechargeRuleRepository.selectrechargeRulesByMerchantIdAndStoredRulesId(new MerchantId(enableRechargeRule.getMerchantId()), new MbrRechargeRuleId(enableRechargeRule.getStoredRulesId()));

        //判断是否启用了10个
        if (count < 10) {

            ///判断启用状态是否 参数是启用状态 查询出的状态是为未启用
            if (enableRechargeRule.getEnable() == 1 && mbrRechargeRule.getEnable().value == 0) {
                mbrRechargeRule.setEnable(Enable.getEnable(enableRechargeRule.getEnable()));
                rechargeRuleRepository.update(mbrRechargeRule);

            } else {
                throw new Exception("储值规则启用已经超过10");
            }

        } else {
            throw new Exception("已经启用10个规则");
        }

    }


    /**
     * 禁用储值规则
     *
     * @param disableRechargeRule 参数
     * @throws Exception 异常
     */
    public void disableRechargeRule(DisableRechargeRule disableRechargeRule) throws Exception {
        //根据商户id 和储值规则id 查询储值规则
        MbrRechargeRule mbrRechargeRule = rechargeRuleRepository.selectrechargeRulesByMerchantIdAndStoredRulesId(new MerchantId(disableRechargeRule.getMerchantId()), new MbrRechargeRuleId(disableRechargeRule.getStoredRulesId()));

        ///判断启用状态是否 是未启用状态 查询出的状态是启用
        if (disableRechargeRule.getEnable() == 0 && mbrRechargeRule.getEnable().value == 1) {
            mbrRechargeRule.setEnable(Enable.getEnable(disableRechargeRule.getEnable()));
            rechargeRuleRepository.update(mbrRechargeRule);
        } else {
            throw new Exception("现在已经是 不启用状态");
        }
    }


    /**
     * 删除储值规则
     *
     * @param deleteRechargeRule
     * @throws Exception
     */
    public void delRechargeRule(DeleteRechargeRule deleteRechargeRule) throws Exception {
        //根据商户id 和储值规则id 查询储值规则
        MbrRechargeRule mbrRechargeRule = rechargeRuleRepository.selectrechargeRulesByMerchantIdAndStoredRulesId(new MerchantId(deleteRechargeRule.getMerchantId()), new MbrRechargeRuleId(deleteRechargeRule.getStoredRulesId()));

        ///判断启用状态是否 是未启用状态 查询出的状态是启用
        if (deleteRechargeRule.getEnable() == 0 && mbrRechargeRule.getEnable().value == 0) {
            mbrRechargeRule.setIsDelete(IsDelete.DELETE);
            rechargeRuleRepository.update(mbrRechargeRule);
        } else {
            throw new Exception("当前为启用状态，不能删除！");
        }
    }


    /**
     * 开启自定义储值开关
     *
     * @param merchantId 商户id
     */
    public void openRechargeRule(Long merchantId) {
        MbrConfig mbrConfig = mbrConfigRepository.findByMerchantId(new MerchantId(merchantId));
        if (mbrConfig != null) {
            mbrConfig.onCustomStored();
            mbrConfigRepository.update(mbrConfig);
        } else {
            MbrConfig config = new MbrConfig(
                    null,
                    new MerchantId(merchantId),
                    null,
                    null,
                    null,
                    new Date(),
                    null);
            mbrConfig.onCustomStored();
            mbrConfigRepository.save(config);
        }
    }

    /**
     * 关闭自定义储值开关
     *
     * @param merchantId 商户id
     */
    public void closeRechargeRule(Long merchantId) {
        MbrConfig mbrConfig = mbrConfigRepository.findByMerchantId(new MerchantId(merchantId));
        if (mbrConfig != null) {
            mbrConfig.offCustomStored();
            mbrConfigRepository.update(mbrConfig);
        } else {
            MbrConfig config = new MbrConfig(
                    null,
                    new MerchantId(merchantId),
                    null,
                    null,
                    null,
                    new Date(),
                    null);
            mbrConfig.offCustomStored();
            mbrConfigRepository.save(config);
        }

    }

}
